﻿/*
 * Created by SharpDevelop.
 * User: Matjaž
 * Date: 13.12.2008
 * Time: 16:29
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Windows.Forms;
using System.Xml;

namespace gopher
{
	/// <summary>
	/// Description of FavouriteName.
	/// </summary>
	public partial class FavouriteName : Form
	{
		private string pserver;
		private string pport;
		private string pselector;
		private string pplus;
		private string ptype;
		private string pfavouritesFile;
		
		public FavouriteName(string favouritesFile, string server, string port, string selector, string plus, string type)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			  pserver = server;
			  pport = port;
			  pselector = selector;
			  pplus = plus;
			  ptype = type;
			  pfavouritesFile = favouritesFile;
		}
		
		void OkButtonClick(object sender, EventArgs e)
		{
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(pfavouritesFile);
				XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "item","");
				XmlAttribute newAttribute = doc.CreateAttribute("name");
				newAttribute.InnerText = this.favouriteNameBox.Text;
				newNode.Attributes.Append(newAttribute);
				
				newAttribute = doc.CreateAttribute("type");
				newAttribute.InnerText = "0";
				newNode.Attributes.Append(newAttribute);
				
				newAttribute = doc.CreateAttribute("server");
				newAttribute.InnerText = pserver;
				newNode.Attributes.Append(newAttribute);
				
				newAttribute = doc.CreateAttribute("port");
				newAttribute.InnerText = pport;
				newNode.Attributes.Append(newAttribute);
				
				newAttribute = doc.CreateAttribute("selector");
				newAttribute.InnerText = pselector;
				newNode.Attributes.Append(newAttribute);
				
				newAttribute = doc.CreateAttribute("plus");
				if(plusBox.CheckState == CheckState.Checked)
				{
					newAttribute.InnerText = "\t$";
				}
				else
				{
					newAttribute.InnerText = "";
				}
				newNode.Attributes.Append(newAttribute);
				
				//newNode.InnerText = textBox1.Text;
				doc.DocumentElement.AppendChild( newNode );

				doc.Save(pfavouritesFile);
				Close();
			}
			catch(Exception err)
			{
				MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				SettingsDialog sd = new SettingsDialog();
				sd.ShowDialog();
			}
		}
		
		void CancelButtonClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void FavouriteNameHelpRequested(object sender, HelpEventArgs hlpevent)
		{
			Help.ShowHelp(this, Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"))+"\\Help\\gopher.chm", "html\\favourites-dialog.htm");
		}
	}
}
